import { useIntersectionObserver } from "@vueuse/core";
import { App } from "vue";
import defaultImg from "@/assets/images/nana.webp";
export default {
  install(app: App) {
    app.directive("imgLazy", {
      mounted(el, binding) {
        const { stop } = useIntersectionObserver(
          el,
          ([{ isIntersecting }], observerElement) => {
            if (isIntersecting) {
              // 当图片url无效加载失败的时候使用默认图片替代
              el.onerror = function () {
                el.src = defaultImg;
              };
              el.src = binding.value;
              stop();
            }
          },
        );
      },
    });
  },
};
